<?php
namespace app\app\controller\v1;
use app\app\model\SignModel;
use app\app\util\OnlineUtils;
use app\app\util\PointUtils;
use think\Db;

class SignController extends BaseController{

    /**
     * 簽到
     */
    public function signs(){
        $user_id = OnlineUtils::getUserIdBySid();
        //判断今天是否已签到
        $start = date('Y-m-d',time())." 00:00:01";
        $end   = date('Y-m-d',time())." 23:59:59";
        $start_time = strtotime($start);
        $end_time   = strtotime($end);
         $signs    = Db::name('user_sign')->where('user_id','eq',$user_id)->where('create_time','between',[$start_time,$end_time])->find();
         if( $signs ){
             return jsonReturn(402,'今日已签到');
         }
        $sign    = new SignModel();
        $sign->user_id = $user_id;
        try {
            $result = $sign->sign();
        } catch (Exception $e) {
            return jsonReturn(402,'签到失败');
        }
        //redis加积分
        PointUtils::getUserPoint($user_id,$result['point']);
        return jsonReturn(200,'success',$result['point']);
    }
}
